package com.hottv.meeting.videomeeting;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Chronometer;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hottv.meeting.videomeeting.common.MeetingConfig;
import com.hottv.meeting.videomeeting.module.edp.data.MeetingInfo;
import com.hottv.meeting.videomeeting.module.meeting.InfoAccessImpl;

/**
 * Created by chengwenqiang on 2019/6/5.
 */

public class IncallActivity extends Activity {

    private Handler mHandler = new Handler();
    private Ringtone mRingtonePlayer;
    private Context mContext;

    private TextView mTxt;

    private Chronometer mCh;

    private String mData;
    private Runnable ringRunnable = new Runnable() {
        @Override
        public void run() {
            if (mRingtonePlayer == null) {
                try {
                    mRingtonePlayer = RingtoneManager.getRingtone(mContext, android.provider.Settings.System.DEFAULT_RINGTONE_URI);
                } catch (Exception e) {
                    e.printStackTrace();
                    return;
                }
            }

            if (mRingtonePlayer != null) {
                synchronized (mRingtonePlayer) {
                    if (mRingtonePlayer.isPlaying()) {
                        mRingtonePlayer.stop();
                    }
                    Log.d(MeetingConfig.TAG, "开始振铃");
                    mRingtonePlayer.play();
                    ring_time = System.currentTimeMillis();

                }
            }
        }
    };
    private long ring_time;

    private void ring() {
        mHandler.postDelayed(ringRunnable, 100);
    }

    private void ringStop() {
        if (mRingtonePlayer != null) {
            synchronized (mRingtonePlayer) {
                Log.d(MeetingConfig.TAG, "停止振铃111");
                mRingtonePlayer.stop();
            }
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layou_call);
        mContext = this;
        mTxt = (TextView) findViewById(R.id.txt_name_incall);

        InfoAccessImpl.getInstance(mContext);

        mCh = (Chronometer) findViewById(R.id.call_time_dration);
        mCh.setBase(SystemClock.elapsedRealtime());
        mCh.start();

        mCh.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
            @Override
            public void onChronometerTick(Chronometer chronometer) {

                // 如果从开始计时到现在超过了60s
                long timeSpan = SystemClock.elapsedRealtime() - mCh.getBase();
                if (timeSpan > 60 * 1000)
                {
                    mCh.stop();
                    Toast.makeText(mContext,"超过时间未接电话",Toast.LENGTH_LONG).show();
                    finish();
                }
            }
        });


        String data = getIntent().getStringExtra(MeetingConfig.MeetingDATA_Incall);
        if(!TextUtils.isEmpty(data))
        {
            mData = data;
            Gson gson =new Gson();
            MeetingInfo info = gson.fromJson(data,MeetingInfo.class);
            mTxt.setText(info.fromUserName+" 邀请您参加会议");
        }
        else
        {
            Log.e(MeetingConfig.TAG,"call activity data is null ");
        }

        ring();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        InfoAccessImpl.getInstance(mContext);
    }

    @Override
    protected  void onDestroy(){
        super.onDestroy();
        onEnd(null);
        InfoAccessImpl.getInstance(mContext).unbindAuthService();
    }

    public void onEnd(View view)
    {
        ringStop();
        this.finish();
    }

    public void onAnswer(View view)
    {
        ringStop();

        Intent intent = new Intent(mContext,Meeting2Activity.class);
        intent.putExtra(MeetingConfig.MeetingDATA_Incall, mData);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );

        mContext.startActivity(intent);
        this.finish();
    }
}
